package eu.telecombretagne.rsm.base.event.manager;

import java.util.HashMap;

import eu.telecombretagne.rsm.base.entity.AltBitEmitterEntity;
import eu.telecombretagne.rsm.base.entity.AppEmitterEntity;
import eu.telecombretagne.rsm.base.entity.Entity;
import eu.telecombretagne.rsm.base.entity.NetworkReaderEmitterEntity;
import eu.telecombretagne.rsm.base.entity.TimerEntity;
import eu.telecombretagne.rsm.base.event.AckEvent;
import eu.telecombretagne.rsm.base.event.AlarmInitEvent;
import eu.telecombretagne.rsm.base.event.AppMessageEvent;
import eu.telecombretagne.rsm.base.event.AppMessageReturnEvent;
import eu.telecombretagne.rsm.base.event.Event;
import eu.telecombretagne.rsm.base.event.FrameEvent;
import eu.telecombretagne.rsm.base.event.TimerExpiredEvent;

public class EventProducerManager {
	
	private static HashMap<Class<? extends Event>, Class<? extends Entity>> eventMainProducers = new HashMap<Class<? extends Event>, Class<? extends Entity>>();
	
	/**
	 * Initialize the manager.
	 */
	public static void init(){
		eventMainProducers.put(AckEvent.class, NetworkReaderEmitterEntity.class);
		eventMainProducers.put(AlarmInitEvent.class, AltBitEmitterEntity.class);
		eventMainProducers.put(AppMessageEvent.class, AppEmitterEntity.class);
		eventMainProducers.put(AppMessageReturnEvent.class, AltBitEmitterEntity.class);
		eventMainProducers.put(FrameEvent.class, AltBitEmitterEntity.class);
		eventMainProducers.put(TimerExpiredEvent.class, TimerEntity.class);
	}
	
	private static void initializeProducersIfEmpty(){
		if(eventMainProducers.isEmpty()){
			init();
		}
	}
	
	/**
	 * Asserts if the given entity is a main producer of the chosen event type.
	 * 
	 * @param pEvent A chosen event type.
	 * @param pEntity A given entity type.
	 * @return Returns <i>true</i> if the given entity is a main producer of the given event type. Otherwise <i>false</i> is returned.
	 */
	public static Boolean assertMainProducer(Class<? extends Event> pEvent, Class<? extends Entity> pEntity){
		Boolean mainProducer = false;
		
		initializeProducersIfEmpty();
		
		if(eventMainProducers.containsKey(pEvent)){
			Class<? extends Entity> entity = eventMainProducers.get(pEvent);
			
			if(entity.equals(pEntity)){
				mainProducer = true;
			}
		}
		
		return mainProducer;
	}
}